package com.ff.neuropp.controller;



import com.ff.neuropp.domain.CourseCategory;
import com.ff.neuropp.service.*;

import com.ff.neuropp.util.ResultTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程类型管理接口
 */
@RestController
@RequestMapping("/api/coursecategory")
@Api(value = "课程类型管理controller", tags = {"课程类型管理接口"})
public class CourseCategoryController {

    @Resource
    private CourseCategoryService courseCategoryService;

    @PostMapping("/create")
//    @RequiredPermission("/api/coursecategory/create")
    @ApiOperation(value = "保存课程类型信息",notes = "保存课程类型信息")
    public Map<String,Object> saveCourseCategory(@RequestBody CourseCategory courseCategory){
        CourseCategory cc = courseCategoryService.saveCourseCategory(courseCategory);
        return ResultTool.genSuccessResult("添加成功",cc);
    }
    @PostMapping("/update")
//    @RequiredPermission("/api/coursecategory/update")
    @ApiOperation(value = "修改课程类型信息", notes = "修改课程类型信息")
    public Map<String,Object> updateCourseCategory(@RequestBody CourseCategory courseCategory){
        CourseCategory cc = courseCategoryService.findById(courseCategory.getId());
        if(null == cc){ // 没查到该条数据
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"编辑错误,无该条数据！");
        }
        CourseCategory updateCc = courseCategoryService.saveCourseCategory(courseCategory);
        return ResultTool.genSuccessResult("修改成功",updateCc);
    }

    @PostMapping("/status")
//    @RequiredPermission("/api/coursecategory/status")
    @ApiOperation(value = "修改课程类型状态", notes = "修改课程类型状态")
    public Map<String,Object> updateCourseCategory(@ApiParam(required = true,value = "类型id") @RequestParam Integer id,
                                                   @ApiParam(required = true,value = "状态") @RequestParam Integer status){
        CourseCategory cc = courseCategoryService.findById(id);
        if(null == cc){ // 没查到该条数据
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"错误,无该条数据！");
        }
        cc.setStatus(status);
        CourseCategory updateCc = courseCategoryService.saveCourseCategory(cc);
        return ResultTool.genSuccessResult("修改成功",updateCc);
    }

    @PostMapping("/delete")
//    @RequiredPermission("/api/coursecategory/delete")
    @ApiOperation(value = "删除课程类型信息", notes = "删除课程类型信息")
    public  Map<String,Object> deleteCourseCategory(@ApiParam(required = true,value = "课程类型id") @RequestParam Integer id){
        CourseCategory cc = courseCategoryService.findById(id);
        if (null == cc) {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "无该条数据");
        }
        //TODO
        //根据类型获取课程列表 如果有记录，则不可以删除 否则可以删除
//        Map<String,Object> sqlParams = new HashMap<>();
//        sqlParams.put("GT_courseCategory.id",id);
//        List<Course> courseList = couseServive.findList(param);
//        if (null != courseList && courseList.size() > 0) {
//            return ResultTool.genFailResult(ResultTool.ErrorCode_2, "正在使用中，不可以删除");
//        }
        courseCategoryService.deleteById(id);
        return ResultTool.genSuccessResult("删除成功");

    }
    @GetMapping("/list")
    @ApiOperation(value = "获取所有课程类型", notes = "获取所有课程类型")
    public Map<String, Object> findList(@ApiParam(required = false, value = "关键词") @RequestParam(required = false) String name,
                                        HttpServletRequest request) {
        Map<String, Object> param = new HashMap<>();

        param.put("GT_id", 0);
        if (null != name && name.length() > 0) {
            param.put("LIKE_name", name);
        }
        List<CourseCategory> list = courseCategoryService.findList(param);
        Map<String, Object> result = new HashMap<>();
        if (null != list && list.size() > 0) {
            result.put("total",list.size());
            result.put("list",list);
            return ResultTool.genSuccessResult("查询成功",result);

        } else {
            return ResultTool.genFailResult(ResultTool.ErrorCode_1, "没有查询到数据");
        }
    }

    @GetMapping("/listpage")
//    @RequiredPermission("/api/coursecategory/listpage")
    @ApiOperation(value = "课程类型分页", notes = "课程类型分页")
    public Map<String, Object> findListPage(@Valid CourseCategory courseCategory,
                                            HttpServletRequest request) {

        Page<CourseCategory> list = courseCategoryService.findPage(courseCategory);
        if(null !=list && list.getTotalElements()>0){
            return ResultTool.genSuccessResult4Page("分页查询成功",list);
        }else{
            return ResultTool.genFailResult(ResultTool.ErrorCode_1,"没有查询到数据");
        }

    }
}
